<?php
/**
 * 名称：TextLinkAction
 * 描述：内链接管理
 * 作者：lan 时间：2014-07-22
 * 版本号：V1.0.0
 * ========================历史版本=================================
 * 序号	 版本号 	日期			姓名	 	描述
 * 1 	V1.0.0 	2013-07-22 	jepin  	站内链接
 * ========================历史版本=================================
 * 说明：请在有更改程序的时候修改以上注释
 */
class TextLinkAction extends PublicAction {
	/**
	 * 文本连接显示列表
	 */
	public function textLink(){
		import("ORG.Util.Page"); // 导入分页类
		if($_GET['text'] != null && $_GET['text'] != "" ){
			$map['text'] = array("like","%".$_GET['text']."%");
			$this->assign("text",$_GET['text']);
			$this->assign("search","yes");
		}
		if(!empty($_GET['siteid'])){
		    $map['siteid'] =intval($_GET['siteid']);
		    $this->assign('siteid',$_GET['siteid']);
		    $this->assign('search','yes');
		}
		
	    $sitelist= M('cms_site')->select();
	    $this->assign('sitelist',$sitelist);
		$count = model('TextLink') -> where($map)->count();
		$page = new Page($count,20);
		$data = model('TextLink')  -> where($map) -> order('link_id') -> limit($page->firstRow . ',' . $page->listRows)->select();
		$show = $page->show(); // 分页显示输出
		$this->assign('page', $show); // 赋值分页输出
		$this->assign('data', $data);
		$this->display();
	}
	
	/**
	 * 新增/编辑文本连接
	 */
	Public function addTextlink(){
		$link_id  = $_GET['link_id'] ;
		if(!empty($link_id)){
		  $textlink= model('TextLink')->where("link_id =".$link_id)->find();
		  $this->assign('textlink',$textlink);	
		}
		$sitelist =M('cms_site')->select();
		$this->assign('sitelist',$sitelist);
 		$this->display();
	}
	/**
	 * 新增、编辑文本连接处理
	 */
	public function doAddTextlink(){
	   $data['link_id'] = $_POST['link_id'];
	   $data['text'] 	= $_POST['text'];
	   $data['url']     = $_POST['url'];
	   $data['siteid']  = $_POST['siteid'];
	   if(empty($data['text'])||empty($data['url'])){
	   	 $this->error('提交数据不能为空！！');
	   }
	   $return = model('TextLink')->addTextlink($data);
	   if($return){
	   	 $this->success("操作成功",URL('cms/TextLink/textLink'));
	   }else{
	   	 $this->error("操作失败",URL('cms/TextLink/textLink'));
	   }
	   
	}
	
	/**
	 * 删除文本连接
	 */
	public function doDelTextlink(){
		if($_POST['ids'] == null || $_POST['ids'] == "" ){
			echo 0;
			exit();
		}
		$ids = $_POST['ids'];
		$res = model('TextLink') -> where("`link_id` in (".$ids.")") -> delete();
		echo $res ? 1 : 0;
	}
    
	/**
	 * 内连接配置
	 */
	public function linkConfig() {
		if($_POST) {
			$configname = 'textlink';
			try {
				$filename = SITE_PATH . '/apps/cms/Conf/'.$configname.'.php';
				file_exists($filename) or touch($filename);
				if($_POST['__hash__']) unset($_POST['__hash__']);
				if($_POST['configname']) unset($_POST['configname']);
				$return = var_export($_POST, TRUE);
				$arr['linkConfig']=$_POST;
				model('Data')->lput64('linkConfig', $arr);
				if ($return) {
					file_put_contents($filename, "<?php \r\n return " . $return . ";");
				}
				$this->success('配置成功',URL('cms/TextLink/linkConfig'));
			} catch (Exception $e) {
				$this->error("请检查文件{$filename}是否有读写权限");
			}
		} else {
			$configname ='textlink';
			$config = include SITE_PATH . '/apps/cms/Conf/'.$configname.'.php';
			if (!$config) {
				$config = array();
			}
			$config['linkcode'] =  common("Html")->text($config['linkcode']);//
			$this->assign("config", $config);
			$this->display();
		}
	}
	
}
?>
